package com.maycurobj.controller.maycur;

import com.alibaba.fastjson.JSONObject;
import com.maycurobj.pojo.MayCurPartner;
import com.maycurobj.service.http.HttpAPIService;
import com.maycurobj.service.impl.MaycurService;
import com.maycurobj.util.DingMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 每刻 往来客商 中台推送
 */
@Controller
@RequestMapping("/maycur/partner")
@Slf4j
public class PartnerPivotController {

    @Autowired
    private MaycurService maycurService;

    @Autowired
    private DingMessageUtil dingMessageUtil;

    /**
     * 接受上游对象 （中台）
     * 推送供应商为主
     * @param partner
     */
    @PostMapping("/push")
    @ResponseBody
    public String push(@RequestBody MayCurPartner partner) {
        Map<String, Object> map = new HashMap<>();
        map.put("phones", new String[]{"15267167276"});
        try {
            String rsp = this.maycurService.tradingPartnerBatch(partner);
            map.put("rsp", rsp);
            dingMessageUtil.pushMessage(null, JSONObject.toJSONString(map));
            return rsp;
        } catch (Exception ex) {
            map.put("msg", "往来客商推送异常（Push），请检查" + ex.toString());
            try {
                dingMessageUtil.pushMessage(null, JSONObject.toJSONString(map));

            } catch (Exception e) {
                e.printStackTrace();
                return "push error";
            }
            return "push error";
        }
    }

    /**
     * 1.用于上游 推送 更新每刻中的往来客商 的账户信息
     * 2.如果推送的往来客商不存在但U8id存在,先创建后推送
     * @param entity
     * @return
     */
    @PostMapping("/pushAccount")
    @ResponseBody
    public void pushAccount(@RequestBody MayCurPartner entity) {
        log.info("打印pushaccount 入参信息---->>>>{}", JSONObject.toJSONString(entity));
        //需要根据erpcode  与 businesscode 进行判断每刻是否已经存在该供应商
        if (StringUtils.isNotBlank(entity.getBusinessCode())) {
            log.info("每刻已经存在");

            //每刻已经存在
            //需要转换下请求账号信息为list
            List<Map<String, String>> _list = new ArrayList<>();
            Map<String, String> _map = new HashMap<>();
            _map.put("accountName", entity.getAccountName());
            _map.put("accountNumber", entity.getAccountNumber());
            _map.put("bankBranchNo", entity.getBankBranchNo());
            _list.add(_map);
            entity.setAccounts(_list);

            String ret = this.maycurService.tradingPartnerBatchAccount(entity);
            if (StringUtils.isNotBlank(ret)) {
                dingMessageUtil.pushMessage(null, ret);

            }else{
                dingMessageUtil.pushMessage(null, "推送往来客商新("+entity.getName()+")账号异常");
            }
        }else{
            log.info("每刻不存在 同时推送供应商及账号");
            //每刻不存在 同时推送供应商及账号
            //改下对象字段 和 账号主表变list
            //需要转换下请求账号信息为list
            List<Map<String, String>> _list = new ArrayList<>();
            Map<String, String> _map = new HashMap<>();
            _map.put("accountName", entity.getAccountName());
            _map.put("accountType", "BANK");
            _map.put("accountNumber", entity.getAccountNumber());
            _map.put("bankBranchNo", entity.getBankBranchNo());
            _list.add(_map);
            entity.setAccounts(_list);
            entity.setBusinessCode(entity.getErpCode());
            //改下存放路径名称
            if(StringUtils.isNotBlank(entity.getParentName())){
                String path="集团外单位,"+entity.getParentName().replace("省","");
                entity.setParentName(path);
            }else{
                entity.setParentName("集团外单位,其他");
            }
            try {
                String ret=maycurService.tradingPartnerBatch(entity);
                dingMessageUtil.pushMessage(null, ret);

            } catch (Exception e) {
                dingMessageUtil.pushMessage(null, "BPM(往来客商交互)补充供应商及账号时 推送异常,请通知管理员");

            }
        }

    }

}
